// example/userinfo/userinfo.js
var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seniorMall:false,
    userInfo: [],
    date: '2016-09-01',
    time: '12:01',
    index: 3,
    array: ['车主', '配件商', '修理厂'],
    objectArray: [
      {
        id: 0,
        name: '车主'
      },
      {
        id: 1,
        name: '配件商'
      },
      {
        id: 2,
        name: '修理厂'
      },
      {
        id: 3,
        name: '请选择会员身份'
      },
    ],
    sexIndex: 0,
    sexArray: ['男', '女'],
    sexObjectArray: [
      {
        id: 0,
        name: '男'
      },
      {
        id: 1,
        name: '女'
      },
    ],
  },
  updateUserInfo() {

    let self = this
    let optrole= ''
    switch(this.data.index){
      case '0':
        optrole = 'OWNER'
        break;
      case '1':
        optrole = 'PARTS'
        break;
      case '2':
        optrole = 'REPAIR'
        break;
    }
    let url = app.globalData.baseUrl1 + '/user/manager/update';
    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId,
      sex: this.data.sexObjectArray[this.data.sexIndex].name,
      birthday: this.data.date,
      role: optrole
  } 
    let callBack = (res) => {
     

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }
      if (res.data.status != 200) {
        wx.showModal({
          title: res.data.message,
          content: '',
          showCancel: false,
          cancelColor: '',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        return;
      }

      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000, success() {

        }
      })

    }
    Api.reqFormPost(url, data, callBack)

  },

  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    this.updateUserInfo()
    
  },

  bindPickerSexChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sexIndex: e.detail.value
    })
    this.updateUserInfo()
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    this.updateUserInfo()
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    this.setData({
      seniorMall: app.globalData.merchant.seniorMall
    })
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  loginOut() {
    let self = this
    let url = app.globalData.baseUrl1 + '/auth/user/loginout'
    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId
    }
    let callBack = (res) => {
      console.log('退出登录', res)
      if (res == null) {
        console.log("网络请求错误1！");
        return;
      }
      if (res.data.status != 200) {
        wx.showModal({
          title: res.data.message,
          content: '',
          showCancel: false,
          cancelColor: '',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })

        return;
      }
      if (res.data.status == 200) {
        wx.reLaunch({
          url: '../../index/index',
          success: function (res) {
            app.globalData.token = null
            app.globalData.userId = null
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  getInfo() {
    let self = this
    let url = app.globalData.baseUrl1 + '/user/info'
    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId
    }
    let callBack = (res) => {
      console.log('用户信息', res)
      if (res == null) {
        console.log("网络请求错误1！");
        return;
      }

      if (res.data.status != 200) {


        return;
      }

      if (res.data.status == 200) {
        this.setData({
          userInfo: res.data.data,
          date: res.data.data.birthday,

        })
        let sexIndex = 0
        switch ((this.data.userInfo.sex||'')) {
          case '男':
            sexIndex = 0
            break;
          case '女':
            sexIndex = 1
            break;
          case '':
            sexIndex = 0
            break;
        }
        let index = 0
        switch (this.data.userInfo.role) {
          case 'OWNER':
            index = 0
            break;
          case 'PARTS':
            index = 1
            break;
          case 'REPAIR':
            index = 2
            break;
        }
        this.setData({
          index: index,
          sexIndex: sexIndex
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    if (app.globalData.merchant.mustLogin || app.globalData.userId === null) {
      if (app.globalData.userId === null) {
        wx.navigateTo({
          url: '/pages/core/login/login',
          success() {
            wx.showToast({
              title: '请先登陆',
              icon: 'loading',
              duration: 1000
            })
          }
        })
        return
      }
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})